<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\tools;

use app\admin\model\tools\Account as AccountModels;
use think\db\Query;

Class Account {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2020-03-17
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'cid') ) {
            $map[] = ['cid', '=', $get['cid']];
        }
        if ( get($get, 'start_datetime') ) {
            $map[] = ['datetime', '>=', $get['start_datetime']];
        }
        if ( get($get, 'end_datetime') ) {
            $map[] = ['datetime', '<=', $get['end_datetime']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return AccountModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 统计
     * @author zx
     * @return mixed
     * @param array $get 筛选条件
     */
    public function account( array $get=[] )
    {
        $map = [];
        if ( get($get, 'cid') ) {
            $map[] = ['cid', '=', $get['cid']];
        }
        if ( get($get, 'start_datetime') ) {
            $map[] = ['datetime', '>=', $get['start_datetime']];
        }
        if ( get($get, 'end_datetime') ) {
            $map[] = ['datetime', '<=', $get['end_datetime']];
        }
        return AccountModels::field('sum(price) as sumPrice, cid')
            ->with(['category'])
            ->where($map)
            ->group('cid')
            ->select();
    }

    /**
     * 新增\编辑信息
     * @param array $data 数据包
     * @author zx
     * @date 2020-03-17
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $account = AccountModels::create($data, ['cid','price','datetime','desc']);
        return $account->isEmpty()?false:$account->id;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @author zx
     * @date 2020-03-17
     * @return boolean
     */
    public function destory( $data )
    {
        return AccountModels::destroy(function(Query $query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

    /**
     * 菜品分类编辑
     * @param AccountModels $model
     * @param array $data
     */
    public function save(AccountModels $model, array $data)
    {
        $model->cid = $data['cid'];
        $model->price = $data['price'];
        $model->datetime = $data['datetime'];
        $model->desc = $data['desc'];
        return $model->save();
    }

    /**
     * 查询分类下是否有账目
     * @param int|array $id
     * @return bool
     */
    public function categoryAcount( $id )
    {
        $map = [];
        if ( is_array($id) ) {
            $map[] = ['cid', 'in', $id];
        } else {
            $map[] = ['cid', '=', $id];
        }
        $count = AccountModels::where($map)->count();
        return $count<=0;
    }

}